package com.example.security.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.security.common.CommonResult;
import com.example.security.dto.User;
import com.example.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    /**
     * 登录以后返回token
     *
     * @param userDTO
     * @param result
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody User userDTO, BindingResult result) {

        String token = userService.login(userDTO.getUsername(), userDTO.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        return CommonResult.success(new HashMap<String, String>() {{
            put("token", token);
            put("tokenHead", tokenHead);
        }});
    }

    @GetMapping("/user/getUser")
    public CommonResult<IPage<User>> getUser() {

        return CommonResult.success(userService.page(new Page()));
    }
    
    @Value("${info.version}")
    private String version;
    
    @GetMapping("/info/version")
    public String version() {

        return version;
    }
}
